
package com.ibeeking.found.open.b.rest.controller.v1;

import com.ibeeking.found.common.annotation.IgnoreResult;
import com.ibeeking.found.open.b.rest.service.IWxCpContactEventService;
import com.ibeeking.found.open.b.rest.service.IWxCpDkEventService;
import com.ibeeking.found.open.b.rest.service.IWxCpEventService;
import com.ibeeking.found.open.b.rest.service.IWxCpTpEventService;
import com.ibeeking.found.open.service.common.param.QueryOpenAuthInfoParam;
import com.ibeeking.found.open.service.common.vo.OpenAuthViewVO;
import com.ibeeking.found.open.service.common.vo.UrlVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @ClassName WxCpTpEventController
 * @Description 企业微信主动调用
 * @Author ibeeking
 * @Date 2021-06-10 16:23
 **/

@RestController
@RequestMapping("/v1/open/api/wxCpEvent")
public class WxCpEventController {

    @Resource
    private IWxCpEventService wxCpEventServiceImpl;

    @Resource
    private IWxCpTpEventService wxCpTpEventServiceImpl;

    @Resource
    private IWxCpDkEventService wxCpDkEventServiceImpl;

    @Resource
    private IWxCpContactEventService wxCpContactEventServiceImpl;

    /**
     * 签名验证
     *
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "企业微信开放平台授权事件--签名验证")
    @GetMapping(value ={"/authCpTicket","/{agentId}/msgCpEvent","/authCpTpTicket","/msgCpTpEvent","/authCpDkTicket","/msgCpDkEvent", "/authCpContactTicket"})
    @IgnoreResult
    public String checkWxCpAuthSignature(HttpServletRequest request, HttpServletResponse response,
                                           @RequestParam(name = "msg_signature", required = false) String signature,
                                           @RequestParam(name = "timestamp", required = false) String timestamp,
                                           @RequestParam(name = "nonce", required = false) String nonce,
                                           @RequestParam(name = "echostr", required = false) String echostr
    ) {
        return wxCpEventServiceImpl.checkWxCpSignature(request,response, signature, timestamp, nonce, echostr);
    }
    /**
     * 企业微信第三方应用授权事件
     *
     * @param echostr 微信发送过来的加密的xml格式数据，通过在创建第三方平台是填写的授权事件URL关联
     *             除了接受授权事件（成功授权、取消授权以及授权更新）外，在接受ticket及授权后回调URI也会用到该方法
     * @return 根据微信开放平台规定，接收到授权事件后只需要直接返回success
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "企业微信第三方应用授权事件")
    @PostMapping(value = "/authCpTicket")
    @IgnoreResult
    public String authCpTicketPost(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(name = "msg_signature", required = false) String signature,
                                     @RequestParam(name = "timestamp", required = false) String timestamp,
                                     @RequestParam(name = "nonce", required = false) String nonce,
                                     @RequestParam(name = "echostr", required = false) String echostr
    ) {
        return wxCpEventServiceImpl.authWxCpTicket(request,response,signature, timestamp, nonce, echostr);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "企业微信第三方应用事件消息")
    @PostMapping(value = "/{agentId}/msgCpEvent")
    @IgnoreResult
    public Object msgCpEvent(HttpServletRequest request, HttpServletResponse response,
                               @PathVariable("agentId") String agentId,
                               @RequestParam("msg_signature") String signature,
                               @RequestParam("timestamp") String timestamp,
                               @RequestParam("nonce") String nonce,
                               @RequestParam(name = "echostr", required = false) String requestBody
    ) {
        return wxCpEventServiceImpl.msgWxCpEvent(request, response, agentId, signature, timestamp, nonce, requestBody);
    }

    /**
     * 企业微信第三方应用授权事件
     *
     * @param echostr 微信发送过来的加密的xml格式数据，通过在创建第三方平台是填写的授权事件URL关联
     *             除了接受授权事件（成功授权、取消授权以及授权更新）外，在接受ticket及授权后回调URI也会用到该方法
     * @return 根据微信开放平台规定，接收到授权事件后只需要直接返回success
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "企业微信第三方应用授权事件")
    @PostMapping(value = "/authCpTpTicket")
    @IgnoreResult
    public String authCpTpTicketPost(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(name = "msg_signature", required = false) String signature,
                                     @RequestParam(name = "timestamp", required = false) String timestamp,
                                     @RequestParam(name = "nonce", required = false) String nonce,
                                     @RequestParam(name = "echostr", required = false) String echostr
    ) {
        return wxCpTpEventServiceImpl.authWxCpTpTicket(request,response,signature, timestamp, nonce, echostr);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "企业微信第三方应用事件消息")
    @PostMapping(value = "/msgCpTpEvent")
    @IgnoreResult
    public Object msgCpTpEvent(HttpServletRequest request, HttpServletResponse response,
                               @RequestParam("msg_signature") String signature,
                               @RequestParam("timestamp") String timestamp,
                               @RequestParam("nonce") String nonce,
                               @RequestParam(name = "echostr", required = false) String requestBody
    ) {
        return wxCpTpEventServiceImpl.msgWxCpTpEvent(request, response, signature, timestamp, nonce, requestBody);
    }

    /**
     * 开放平台授权事件
     *
     * @param echostr 微信发送过来的加密的xml格式数据，通过在创建第三方平台是填写的授权事件URL关联
     *             除了接受授权事件（成功授权、取消授权以及授权更新）外，在接受ticket及授权后回调URI也会用到该方法
     * @return 根据微信开放平台规定，接收到授权事件后只需要直接返回success
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "企业微信开放平台授权事件")
    @PostMapping(value = "/authCpDkTicket")
    @IgnoreResult
    public String authCpDkTicketPost(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(name = "msg_signature", required = false) String signature,
                                     @RequestParam(name = "timestamp", required = false) String timestamp,
                                     @RequestParam(name = "nonce", required = false) String nonce,
                                     @RequestParam(name = "echostr", required = false) String echostr
    ) {
        return wxCpDkEventServiceImpl.authWxCpDkTicket(request,response,signature, timestamp, nonce, echostr);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "企业微信开放平台事件消息")
    @PostMapping(value = "/msgCpDkEvent")
    @IgnoreResult
    public Object msgCpDkEvent(HttpServletRequest request, HttpServletResponse response,
                               @RequestParam("msg_signature") String signature,
                               @RequestParam("timestamp") String timestamp,
                               @RequestParam("nonce") String nonce,
                               @RequestParam(name = "echostr", required = false) String requestBody
    ) {
        return wxCpDkEventServiceImpl.msgWxCpDkEvent(request, response, signature,timestamp, nonce, requestBody);
    }

    /**
     * 开放平台授权事件
     *
     * @param echostr 微信发送过来的加密的xml格式数据，通过在创建第三方平台是填写的授权事件URL关联
     *             除了接受授权事件（成功授权、取消授权以及授权更新）外，在接受ticket及授权后回调URI也会用到该方法
     * @return 根据微信开放平台规定，接收到授权事件后只需要直接返回success
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "企业微信开放平台授权事件")
    @PostMapping(value = "/authCpContactTicket")
    @IgnoreResult
    public String authCpContactTicketPost(HttpServletRequest request, HttpServletResponse response,
                                     @RequestParam(name = "msg_signature", required = false) String signature,
                                     @RequestParam(name = "timestamp", required = false) String timestamp,
                                     @RequestParam(name = "nonce", required = false) String nonce,
                                     @RequestParam(name = "echostr", required = false) String echostr
    ) {
        return wxCpContactEventServiceImpl.authWxCpContactTicket(request,response,signature, timestamp, nonce, echostr);
    }

    /**
     * 授权通讯录
     *
     * @param corpAppId
     * @param appSecret
     * @return
     * @throws Exception
     * @Author ibeeking
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "授权通讯录")
    @PostMapping(value = "/authWxCpDkContactList/{corpAppId}/{appSecret}")
    public Boolean authWxCpDkContactList(@PathVariable("corpAppId") String corpAppId, @PathVariable("appSecret") String appSecret) throws Exception {
        return wxCpContactEventServiceImpl.authWxCpDkContactList(corpAppId, appSecret);
    }

    /**
     * 开放平台授权事件
     *
     * @param echostr 微信发送过来的加密的xml格式数据，通过在创建第三方平台是填写的授权事件URL关联
     *             除了接受授权事件（成功授权、取消授权以及授权更新）外，在接受ticket及授权后回调URI也会用到该方法
     * @return 根据微信开放平台规定，接收到授权事件后只需要直接返回success
     */
    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "企业微信第三方应用通讯录授权事件")
    @PostMapping(value = "/contactAuthTicket", produces = "text/plain;charset=utf-8")
    @IgnoreResult
    public String authCpTpContactTicketPost(HttpServletRequest request, HttpServletResponse response,
                                            @RequestParam(name = "msg_signature", required = false) String signature,
                                            @RequestParam(name = "timestamp", required = false) String timestamp,
                                            @RequestParam(name = "nonce", required = false) String nonce,
                                            @RequestParam(name = "echostr", required = false) String echostr
    ) {
        return wxCpContactEventServiceImpl.authWxCpContactTicket(request,response,signature, timestamp, nonce, echostr);
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "企业微信第三方应用通讯录事件消息")
    @PostMapping(value = "/contactMsgEvent", produces = "text/plain;charset=utf-8" )
    @IgnoreResult
    public Object msgCpTpContactEvent(HttpServletRequest request, HttpServletResponse response,
                                      @RequestParam("msg_signature") String signature,
                                      @RequestParam("timestamp") String timestamp,
                                      @RequestParam("nonce") String nonce,
                                      @RequestParam(name = "echostr", required = false) String requestBody
    ) {
        return wxCpContactEventServiceImpl.msgWxCpContactEvent(request, response, signature, timestamp, nonce, requestBody);
    }


    /**
     * 授权注册页面扫码授权   显示二维码
     *
     * @param request
     * @return
     * @throws Exception
     * @Author ibeeking
     * param appId 微信公众平台的APPID
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取企业微信授权页面url")
    @PostMapping(value = "/getWxCpDkPreAuthUrl")
    public UrlVO getWxCpDkPreAuthUrl(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return wxCpDkEventServiceImpl.getWxCpDkPreAuthUrl(request, response);
    }

    /**
     * 授权注册页面扫码授权   显示二维码
     *
     * @param request
     * @return
     * @throws Exception
     * @Author ibeeking
     * param appId 微信公众平台的APPID
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取企业微信授权页面url")
    @PostMapping(value = "/getWxCpTpPreAuthUrl")
    public UrlVO getWxCpTpPreAuthUrl(HttpServletRequest request) throws Exception {
        return wxCpTpEventServiceImpl.getWxCpTpPreAuthUrl(request);
    }

    /**
     * 获取永久授权码
     *
     * @param param
     * @return
     * @throws Exception
     * @Author ibeeking
     * param appId 微信公众平台的APPID
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取永久授权码")
    @PostMapping(value = "/getWxCpTpPermanentAuthInfo")
    public OpenAuthViewVO getWxCpTpPermanentAuthInfo(@RequestBody @Validated QueryOpenAuthInfoParam param) throws Exception {
        return wxCpTpEventServiceImpl.getWxCpTpPermanentAuthInfo(param);
    }
    /**
     * 授权注册页面扫码授权   显示二维码
     *
     * @param request
     * @return
     * @throws Exception
     * @Author ibeeking
     * param appId 微信公众平台的APPID
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取企业微信授权页面url")
    @PostMapping(value = "/getWxCpContactPreAuthUrl")
    public UrlVO getWxCpContactPreAuthUrl(HttpServletRequest request) throws Exception {
        return wxCpContactEventServiceImpl.getWxCpContactPreAuthUrl(request);
    }

    /**
     * 获取永久授权码
     *
     * @param param
     * @return
     * @throws Exception
     * @Author ibeeking
     * param appId 微信公众平台的APPID
     */
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "获取永久授权码")
    @PostMapping(value = "/getWxCpContactPermanentAuthInfo")
    public OpenAuthViewVO getWxCpContactPermanentAuthInfo(@RequestBody @Validated QueryOpenAuthInfoParam param) throws Exception {
        return wxCpContactEventServiceImpl.getWxCpContactPermanentAuthInfo(param);
    }

}